<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商品分类管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<link rel="stylesheet" href="../plugins/select2/select2.css" />
	<link rel="stylesheet" href="../plugins/select2/select2-bootstrap.css" />
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
	<script src="../plugins/select2/select2.min.js" type="text/javascript"></script>
    
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品分类管理     
                       	</h3>
                    </div>

  <div class="box-body">
	  <ol class="breadcrumb" id="fenlei">
		  <li>
			  <a onclick=" getData(0)" >顶级分类列表</a>
		  </li>
	  </ol>
	  <!-- 数据表格 -->
	  <div class="table-box">

		  <!--工具栏-->
		  <div class="pull-left">
			  <div class="form-group form-inline">
				  <div class="btn-group">
					  <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
					  <button type="button" class="btn btn-default" title="删除" onclick="deleteItemType()"><i class="fa fa-trash-o" ></i> 删除</button>
					  <button type="button" class="btn btn-default" title="刷新" ><i class="fa fa-check"></i> 刷新</button>

				  </div>
			  </div>
		  </div>


		  <!--数据列表-->
		  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			  <thead>
			  <tr>
				  <th class="" style="padding-right:0px">
					  <input type="checkbox" class="icheckbox_square-blue">
				  </th>
				  <th class="sorting_asc">分类ID</th>
				  <th class="sorting">分类名称</th>
				  <th class="sorting">类型模板ID</th>

				  <th class="text-center">操作</th>
			  </tr>
			  </thead>
			  <tbody id="table">

			  </tbody>
		  </table>
		  <!--数据列表/-->
	  </div>
	  <!-- 数据表格 /-->
  </div>


<!-- zengjia编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品分类编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>上级商品分类</td>
		      		<td>
		      		   我不会写这个
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>商品分类名称</td>
		      		<td><input  class="form-control" placeholder="商品分类名称" id="names" name="names">  </td>
		      	</tr>			  
		      	<tr>
		      		<td>类型模板</td>
		      		<td>	      		
						<select id="brand"  style="width: 100%">

						</select>
		      		</td>		      		      		
		      	</tr>		      	
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="toadd()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

  <!-- xiugai编辑窗口 -->
  <div class="modal fade" id="editModals" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" >
  	<div class="modal-content">
  		<div class="modal-header">
  			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
  			<h3 id="myModalLabels">商品分类编辑</h3>
  		</div>
  		<div class="modal-body">

  			<table class="table table-bordered table-striped"  width="800px">
  				<tr>
  		      		<td>上级商品分类</td>
  		      		<td>
						<input type="hidden" id="ids" name="ids">
  		      		   我不会写这个
  		      		</td>
  		      	</tr>
  		      	<tr>
  		      		<td>商品分类名称</td>
  		      		<td><input  class="form-control" placeholder="商品分类名称" id="namess" name="namess">  </td>
  		      	</tr>
  		      	<tr>
  		      		<td>类型模板</td>
  		      		<td>
  						<select id="brands"  style="width: 100%">

  						</select>
  		      		</td>
  		      	</tr>
  			 </table>

  		</div>
  		<div class="modal-footer">
  			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updatas()">保存</button>
  			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
  		</div>
  	  </div>
  	</div>
  </div>
</body>
</html>

<script>

	/*页面加载*/
	$(function () {
		getBrandsList();
		getData();
	})
	function getData(id) {
		$.ajax({
 		    url:"/TbItemCatController/select",
 		    type:"post",
			data:{id:id},
 		    dataType:"json",
 		    success:function (parameter) {
			var list = parameter.data;
			var tr = "";
			for(var i = 0;i < list.length;i++){
				tr += "<tr>"
				tr += '<td><input name="boxs" value="'+list[i].id+'" type="checkbox" ></td>'
				tr += '<td><input readonly="readonly" type="text" id="parentId" name="parentId" value="'+list[i].id+'"></td>'
				tr += '<td>'+list[i].name+'</td>'
				tr += '<td>'+list[i].typeId+'</td>'
				tr += '<td class="text-center">'
				tr +='<button type="button" class="btn bg-olive btn-xs"onclick="getData('+list[i].id+')" >查询下级</button>'
				tr += ' <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModals" onclick="updata('+list[i].id+')">修改</button>'
				tr += '<button type="button" class="btn bg-olive btn-xs" data-toggle="modal"  onclick="del(' + list[i].id + ')">删除</button>'
				tr += ' </td>'
				tr += "</tr>"
			}
				$("#table").html(tr);

 		    },
 		    error:function () {
 		        alert("代码错误")
 		    }
 		})
	}

	function getBrandsList() {
		$.ajax({
			url:"/TbTypeTemplateController/listTemplate",
			type:"post",
			dataType:"json",
			success:function (parameter) {
				console.log(parameter)
				if(parameter.code == 100){
					var option = "";
					var list = parameter.data;
					for (var i = 0;i < list.length; i++){
						option += '<option value="'+list[i].id+'">'
						option += list[i].name;
						option += '</option >'
					}
					$("#brand").html(option);
					$("#brand").select2();
					$("#brands").html(option);
					$("#brands").select2();
				}
			},
			error:function () {
				alert("代码错误")
			}
		})
	}
	
	function toadd() {
		var names = $("#names").val();
		var brands = $("#brand").val();
		/*var parentIds = $("#parentId").val();*/
		var parentIds = $("[name=boxs]:checked").val();;
		$.ajax({
			url:"/TbItemCatController/toAdd",
			type:"post",
			data:{parentId:parentIds,name:names,typeId:brands},
			dataType:"json",
			success:function (parameter) {
			if(parameter){
				alert("增加成功")
			}else{
				alert("增加失败")
			}
			},
			error:function () {
				alert("代码错误")
			}
		})

	}

	/*删除*/
	function deleteItemType(){
		alert("a")
		var ids = [];
		$("[name=boxs]:checked").each(function (i,e){
			ids.push($(e).val());
		})
		if(ids.length <= 0){
			alert("请选择要删除的数据");
			return ;
		}
		$.ajax({
			url:"/TbItemCatController/deleteItemType",
			type:"post",
			data:{ids:ids},
			dataType:"json",
			success:function (result){
				alert("chenggong");
			},
			error:function (){
				alert("网络异常");
			}
		})
	}


	function updata(id) {
		alert("回显方法"+id);
   		$.ajax({
   		    url:"/TbItemCatController/updata?id="+id,
   		    type:"post",
   		    dataType:"json",
   		    success:function (parameter) {
				var str = parameter.data;
				$("#brands").val(str.typeId).trigger("change");
				$("#namess").val(str.name);
				$("#ids").val(str.id);
   		    },
   		    error:function () {
   		        alert("代码错误")
   		    }
   		})

	}

	function updatas() {
		var names = $("#namess").val();
		var brands = $("#brands").val();
		var idss = $("#ids").val();
		$.ajax({
			url:"/TbItemCatController/updatas",
			type:"post",
			data:{id:idss,name:names,typeId:brands},
			dataType:"json",
			success:function (parameter) {
			if(parameter){
				alert("增加成功")
			}else{
				alert("增加失败")
			}
			},
			error:function () {
				alert("代码错误")
			}
		})

	}
</script>